//
//  LBBasicInputTextView.m
//  app
//
//  Created by 杨庆人 on 16/12/23.
//  Copyright © 2016年 广东联结电子商务有限公司. All rights reserved.
//

#import "LBBasicInputTextView.h"
#import "LBLimitedTextField.h"
#import <Masonry/Masonry.h>
#import <LBCategory/LBCategory.h>

@interface LBInputTextView ()<UITextViewDelegate,LBLimitedTextViewDelegate>

@property (nonatomic,assign)NSInteger limit;
@end

@implementation LBInputTextView

- (instancetype)initWithLimit:(NSInteger)limit {
    if (self = [super init]) {
        _limit = limit;
        [self setBasicInputTextView];
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
}
- (void)setText:(NSString *)text {
    [super setText:text];
    !self.NumChageBlock ? :self.NumChageBlock(text.length);
}
- (void)setBasicInputTextView {
    
    self.textColor = [UIColor blackColor];
    self.font = [UIFont systemFontOfSize:14];
    self.returnKeyType = UIReturnKeyDefault;
    self.delegate = self;
    self.layer.cornerRadius = 8;
    self.maxTextLength = self.limit;
    self.lb_degegate = self;
}

-(void)limitedTextView:(LBLimitedTextView *)limitedTextView textCount:(NSInteger)textCount
{
    !self.NumChageBlock ? :self.NumChageBlock(textCount);
}

@end



@interface LBBasicInputTextView ()

@property (nonatomic,strong)UIImageView * bgImageView;
@property (nonatomic,assign)NSInteger limit;
@property (nonatomic,copy)NSString * placeholder;


@end

@implementation LBBasicInputTextView

- (instancetype)initWithPlaceholder:(NSString *)placeholder limit:(NSInteger)limit {
    if (self = [super init]) {
        _placeholder = placeholder;
        _limit = limit;
        [self setBasicInputTextView];
    }
    return self;
}
- (LBInputTextView *)inputTextView {
    if (!_inputTextView) {
        _inputTextView = [[LBInputTextView alloc] initWithLimit:_limit];
        __weak typeof(self)weakSelf = self;
        _inputTextView.NumChageBlock = ^(NSInteger strNum){

            weakSelf.limitLabel.text = [NSString stringWithFormat:@"%ld/%ld",(long)strNum,(long)weakSelf.limit];
        };
    }
    return _inputTextView;
}
- (UIImageView *)bgImageView {
    if (!_bgImageView) {
        _bgImageView = [[UIImageView alloc] init];
    }
    return _bgImageView;
}
- (UILabel *)limitLabel {
    if (!_limitLabel) {
        _limitLabel = [[UILabel alloc] init];
        _limitLabel.text = [NSString stringWithFormat:@"0/%ld",(long)_limit];
        _limitLabel.textColor = [UIColor colorWithRed:147/255.0 green:147/255.0 blue:147/255.0 alpha:1];
        _limitLabel.font = [UIFont systemFontOfSize:12];
        _limitLabel.textAlignment = NSTextAlignmentRight;
    }
    return _limitLabel;
}



- (void)layoutSubviews {
    [super layoutSubviews];
    [self.bgImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(self);
    }];
    [self.inputTextView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.mas_equalTo(self);
        make.bottom.mas_equalTo(self).offset(-20);
    }];

    [self.limitLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.left.mas_equalTo(self.bgImageView);
        make.top.mas_equalTo(self.inputTextView.mas_bottom);
        make.right.mas_equalTo(self).offset(-10);
    }];


}
- (void)setBasicInputTextView {
    self.layer.borderWidth = 1 / [UIScreen mainScreen].scale;
    self.layer.borderColor = [UIColor colorWithR:225 G:229 B:232 A:1].CGColor;
    self.layer.cornerRadius = 2;
    self.backgroundColor = [UIColor whiteColor];
    self.inputTextView.placeholder = self.placeholder;
    
    [self addSubview:self.bgImageView];
    [self addSubview:self.inputTextView];
    [self addSubview:self.limitLabel];
}


@end


